package com.zdp.netty.chapter8;

import com.google.protobuf.InvalidProtocolBufferException;
import com.zdp.netty.chapter8.protobuf.SubscribeReqProto;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author zhengdp
 * @Description
 */
public class TestSubscribeReqProto {

    private static byte[] encode(SubscribeReqProto.SubscribeReq req) {
        return req.toByteArray();
    }

    private static SubscribeReqProto.SubscribeReq decode(byte[] body) throws InvalidProtocolBufferException {
        return SubscribeReqProto.SubscribeReq.parseFrom(body);
    }

    private static SubscribeReqProto.SubscribeReq createSubscribeReq() {
        SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder();
        builder.setSubReqId(1).setUserName("Zhengdp").setProductName("Netty Book");
        List<String> address = new ArrayList<>();
        address.add("NanJing YuHuaTai");
        address.add("BeiJing LiuLiChang");
        address.add("ShenZhen HongShuLin");
        builder.addAllAddress(address);
        return builder.build();
    }

    public static void main(String[] args) throws InvalidProtocolBufferException {
        SubscribeReqProto.SubscribeReq subscribeReq = createSubscribeReq();
        System.out.println("Before encode : " + subscribeReq.toString());
        SubscribeReqProto.SubscribeReq req2 = decode(encode(subscribeReq));
        System.out.println("After decode : " + req2.toString());
        System.out.println("Assert equals :  --> " + req2.equals(subscribeReq));
    }

}
